package ch.ethz.fcl.metrobuzz.render.interchange.temporal;

import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.MBProperty;
import ch.ethz.fcl.metrobuzz.data.MBData;
import ch.ethz.fcl.metrobuzz.render.techniques.circos.CircosMatrix;
import ch.ethz.fcl.metrobuzz.render.techniques.circos.version3.CircosDiagram3;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Rectangle;
import ch.ethz.fcl.metrofont.MetroFont;
import ch.ethz.fcl.metrofont.Pen;

public class TemporalElement {
	private String label = null;
	private CircosDiagram3 circos;
	private Rectangle bound;
	private int startTime;
	private int endTime;

	public TemporalElement(String label, CircosMatrix data, Point2D centroid,
			float radius) {
		this.label = label;
		init(data, centroid, radius);
	}

	public TemporalElement(CircosMatrix data, Point2D centroid, float radius) {
		init(data, centroid, radius);
	}

	public void render(MetroFont font) {
		if (circos == null)
			return;

		String startTimeLabel = getTimeLabel(startTime);
		String endTimeLabel = getTimeLabel(endTime);

		if (startTimeLabel.equals(endTimeLabel))
			return;

		String time = startTimeLabel + "---" + endTimeLabel;

		GL11.glColor4f(1, 1, 1, 1);
		bound.render(GL11.GL_QUADS);

		GL11.glColor4f(0, 0, 0, 1);
		GL11.glLineWidth(1.0f);
		bound.render(GL11.GL_LINE_STRIP);

		float boundWidth = bound.getWidth();
		float x = circos.getCentroid().getX()
				- font.getLength(time, boundWidth / 10) / 2;
		float y = circos.getCentroid().getY() - boundWidth / 2 - boundWidth
				/ 10;
		GL11.glColor4f(0, 0, 0, 1);

		font.display(time, new Pen(x, y, boundWidth / 10));

		if (label != null) {
			x = circos.getCentroid().getX()
					- font.getLength(label, boundWidth / 10) / 2;
			y = circos.getCentroid().getY() + boundWidth / 2 + boundWidth / 10;
			font.display(label, new Pen(x, y, boundWidth / 10));
		}

		if (MBProperty.SHOW_STATISTICS)
			circos.render(font);
		else
			circos.render();
	}

	private void init(CircosMatrix data, Point2D centroid, float radius) {
		circos = new CircosDiagram3(centroid, radius, data);

		float halfBoundSize = 0;
		if (MBProperty.SHOW_STATISTICS)
			halfBoundSize = radius * 1.8f;
		else
			halfBoundSize = radius * 1.3f;
		if (label == null)
			bound = new Rectangle(centroid.getX() - halfBoundSize,
					centroid.getY() - halfBoundSize - halfBoundSize / 4,
					centroid.getX() + halfBoundSize, centroid.getY()
							+ halfBoundSize);
		else
			bound = new Rectangle(centroid.getX() - halfBoundSize,
					centroid.getY() - halfBoundSize - halfBoundSize / 4,
					centroid.getX() + halfBoundSize, centroid.getY()
							+ halfBoundSize + halfBoundSize / 2);
	}

	public CircosDiagram3 getCircos() {
		return circos;
	}

	public void setStartTime(int startTime) {
		this.startTime = startTime;
	}

	public void setEndTime(int endTime) {
		this.endTime = endTime;
	}

	public Rectangle getBound() {
		return bound;
	}

	private String getTimeLabel(int time) {
		String timeLabel = "";
		int hour = time / 4;
		if (time < 0)
			timeLabel += "00";
		else {
			if (hour < 10)
				timeLabel += ("0" + hour);
			else if (hour < 24)
				timeLabel += ("" + hour);
			else
				timeLabel += ("" + 24);
		}

		timeLabel += ":";

		if (time >  MBData.ONE_DAY_TIME || time < 0)
			timeLabel += "00";
		else {
			int min = time % 4;
			if (min == 0)
				timeLabel += "00";
			else if (min == 1)
				timeLabel += "15";
			else if (min == 2)
				timeLabel += "30";
			else
				timeLabel += "45";
		}
		return timeLabel;
	}
}
